<?php

namespace app\admin\dao;

use app\common\model\KnowledgeCategory;
use ShidianUtil\Thinkphp\Dao\DaoTrait;

/**
 * KnowledgeCategoryDao 数据访问对象
 * Class KnowledgeCategoryDao
 * @package app\dao
 * @method KnowledgeCategory findByPK(?int $id, $field = true)
 * @method \think\Collection selectAll(?int $page, ?int $page_size, $field = true)
 * @method KnowledgeCategory insertActive(array $params, array $allow_field = [])
 * @method KnowledgeCategory updateActive(KnowledgeCategory $model, array $params, array $allow_field = [])
 * @method KnowledgeCategory findAndUpdateActive(int $id, array $params, array $allow_field = [])
 * @method KnowledgeCategory updateActiveByClosure(\Closure $closure, array $params, array $allow_field = [])
 * @method void deleteModel(KnowledgeCategory $model)
 * @method void deleteById(int|array|\Closure $id, bool $force = false)
 * @link https://www.kancloud.cn/manual/thinkphp6_0/1037579
 * @link https://www.kancloud.cn/manual/think-orm/1257998
 * @link https://www.kancloud.cn/thinkphp/master-database-and-model/265551
 */
class KnowledgeCategoryDao
{
    use DaoTrait;

    /**
     * @var KnowledgeCategory
     */
    private $baseModel;

    /**
     * KnowledgeCategoryDao constructor.
     */
    public function __construct()
    {
        $this->baseModel = KnowledgeCategory::class;
    }

    /**
     * 法塘分类列表
     * @param int|null $page
     * @param int|null $page_size
     * @return array
     */
    public function listCategories(?int $page, ?int $page_size): array
    {
        $query = KnowledgeCategory::withCount(['knowledges'])
            ->order('ord', 'ASC')
            ->order('id', 'desc')
            ->append(['smallPicImg', 'smallPicImgMin'])
            ->paging($page, $page_size);

        return listOutput($query, $page_size);
    }

    /**
     * 法塘分类详情
     * @param $id
     * @return KnowledgeCategory|null
     */
    public function findCategory($id): ?KnowledgeCategory
    {
        return KnowledgeCategory::withCount(['knowledges'])
            ->where('id', $id)
            ->append(['smallPicImg'])
            ->find();
    }
}
